package cat.joronya.supermanager.service;

import java.util.ArrayList;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.Supermanager;
import cat.joronya.supermanager.team.MyTeamsListActivity;

/**
 * Class that handles close of changes notifications.
 * @author pol
 *
 */
public class ClosedChangesNotification 
{
	public static final String TAG = "supermanager";
	public Context context;
	
	private int injured = 0;
	private int discarded = 0;
	private ArrayList<String> injuredNames = new ArrayList<String>();
	private ArrayList<String> discardedNames = new ArrayList<String>();
	
	public ClosedChangesNotification(Context theContext)
	{
		Log.d(TAG,"ClosedChangesNotification: executing alarm");
		this.context = theContext;
	}
	
	/**
	 * Method to execute.
	 */
	public void chekForNotification()
	{
		Cursor cursor = context.getContentResolver().query(
				Supermanager.Home.CONTENT_URI, 
				Supermanager.Home.PROJECTION, 
				Supermanager.Home.DUE_TIME+" > "+(System.currentTimeMillis()/1000), 
				null, 
				Supermanager.Home.DUE_TIME+" DESC");
		
		// hi ha alguna data? Hem arribat al moment de mostrar la notificacio que s'escaigui
		if( cursor.moveToFirst() )
		{
			int nextDay = cursor.getInt(5);
			long duetime = cursor.getInt(4);
			long now = System.currentTimeMillis()/1000;
			long min = (duetime-now)/60;
			long hours = min/60;
			Log.d(TAG,"Duetime en "+hours+" horas ");
			
			Cursor mtCursor = context.getContentResolver().query(
				Supermanager.MyTeamDetail.CONTENT_URI,
				new String[]{
					Supermanager.MyTeamDetail.STATUS, 		// 0
					Supermanager.MyTeamDetail.ID_EQU,		// 1
					Supermanager.MyTeamDetail.CODI_PLAYER,	// 2
					Supermanager.MyTeamDetail.NAME			// 3
				},
				Supermanager.MyTeamDetail.DAY + " = ? AND ("+
				Supermanager.MyTeamDetail.STATUS + " = 'lesio.png' OR "+
				Supermanager.MyTeamDetail.STATUS + " = 'baixa.png') ",
				new String[]{ nextDay+"" },
				Supermanager.MyTeamDetail.NAME + " ASC "
			);
			
			// contem cadascun 
			parseInjuredAndDiscardedPlayers(nextDay);
			
			// desglosem per detallar missatge notification
			if( injured == 0 && discarded == 0)
			{
				// preparem notification amb pending intent generic
				// apuntant al myteam list, obrim app i prou
				showDuetimeNotification(hours);
			}
			else if( injured == 1 && discarded == 0 )
			{
				// show notification of injured player and duetime 
				showOneAndZeroNotification( injuredNames.get(0), hours);
			}
			else if( injured == 1 && discarded == 1 )
			{
				// show notification of one injured and one discarded
				showOneAndOneNotification(injuredNames.get(0), discardedNames.get(0), hours);
			}
			else if( injured == 1 && discarded > 1)
			{
				// show notification of one injured and many discarded
				showOneAndMoreNotification(injuredNames.get(0), discarded, hours);
			}
			else if( injured == 0 && discarded > 1 )
			{
				// show notification of many discarded
				showZeroAndMoreNotifications(discarded, hours);
			}
			else if( discarded == 1 && injured == 0 )
			{
				// show notification of one discarded
				showZeroAndOneNotifications(discardedNames.get(0), hours);
			}
			else if( discarded == 1 && injured > 1 )
			{
				// show notification of one discarded and many discarded
				showMoreAndOneNotifications(injured, discardedNames.get(0), hours);
			}
			else if( discarded == 0 && injured > 1 )
			{
				// show notification of many injured
				showMoreAndZeroNotifications(injured, hours);
			}
			else
			{
				// show notification of many injured and many discarded
				showMoreAndMoreNotifications(injured, discarded, hours);
			}
		}
	}
	
	/**
	 * Processes count of injured and discarded players just to know what to
	 * notify.
	 * @param day
	 */
	protected void parseInjuredAndDiscardedPlayers(int day)
	{
		Cursor mtCursor = context.getContentResolver().query(
		Supermanager.MyTeamDetail.CONTENT_URI,
			new String[]{
				Supermanager.MyTeamDetail.STATUS, 		// 0
				Supermanager.MyTeamDetail.ID_EQU,		// 1
				Supermanager.MyTeamDetail.CODI_PLAYER,	// 2
				Supermanager.MyTeamDetail.NAME			// 3
			},
			Supermanager.MyTeamDetail.DAY + " = ? AND ("+
			Supermanager.MyTeamDetail.STATUS + " = 'lesio.png' OR "+
			Supermanager.MyTeamDetail.STATUS + " = 'baixa.png') ",
			new String[]{ day+"" },
			Supermanager.MyTeamDetail.NAME + " ASC "
		);
		
		// if there's no players on cursor, no need to parse
		if( !mtCursor.moveToFirst() )
			return;
		
		// count players in each status
		do
		{
			if( "lesio.png".equals(mtCursor.getString(0)) )
			{
				injured++;
				injuredNames.add(mtCursor.getString(3));
				continue;
			}
			if( "baixa.png".equals(mtCursor.getString(0)) )
			{
				discarded++;
				discardedNames.add(mtCursor.getString(3));
				continue;
			}
		}
		while( mtCursor.moveToNext() );
	}
	
	/**
	 * Method to show notification of just one player in your teams, throw team list.
	 */
	protected void showOneAndZeroNotification(String name, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_one_injured_notification_title);
		String text = context.getString(R.string.changes_one_injured_notification_text, name, hours+"");
		String ticker = context.getString(R.string.changes_one_injured_notification_ticker, name);
		
		sendNotification(title, text, ticker);
	}
	
	protected void showOneAndOneNotification(String injuredName, String discardedName, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_one_injured_one_discarded_notification_title);
		String text = context.getString(R.string.changes_one_injured_one_discarded_notification_text, injuredName, discardedName, hours+"");
		String ticker = context.getString(R.string.changes_one_injured_one_discarded_notification_ticker, injuredName, discardedName);
		
		sendNotification(title, text, ticker);
	}

	/**
	 * Method to show notification when many players are injured in your teams.
	 * The pending intent points to my team list.
	 * @param injuredPlayers
	 * @param hours
	 */
	protected void showMoreAndZeroNotifications( int injuredPlayers, long hours )
	{
		// notification texts
		String title = context.getString(R.string.changes_many_injured_notification_title);
		String text = context.getString(R.string.changes_many_injured_notification_text, injuredPlayers+"", hours+"");
		String ticker = context.getString(R.string.changes_many_injured_notification_ticker, injuredPlayers+"");
		
		sendNotification(title, text, ticker);
	}

	/**
	 * Mathod to show notification when one injured and many discarded in your teams.
	 * @param injuredName
	 * @param discarded
	 * @param hours
	 */
	protected void showOneAndMoreNotification(String injuredName, int discarded, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_one_injured_many_discarded_notification_title);
		String text = context.getString(R.string.changes_one_injured_many_discarded_notification_text, injuredName, discarded+"", hours+"");
		String ticker = context.getString(R.string.changes_one_injured_many_discarded_notification_ticker, injuredName, discarded);
		
		sendNotification(title, text, ticker);
	}
	
	/**
	 * Method to show notification when many discarded in your teams.
	 * @param discarded
	 * @param hours
	 */
	protected void showZeroAndMoreNotifications(int discarded, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_many_discarded_notification_title);
		String text = context.getString(R.string.changes_many_discarded_notification_text, discarded+"");
		String ticker = context.getString(R.string.changes_many_discarded_notification_ticker, discarded+"", hours+"");
		
		sendNotification(title, text, ticker);
	}
	
	/**
	 * Method to show notification when one discarded in your teams.
	 * @param discardedName
	 * @param hours
	 */
	protected void showZeroAndOneNotifications(String discardedName, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_one_discarded_notification_title);
		String text = context.getString(R.string.changes_one_discarded_notification_text, discardedName);
		String ticker = context.getString(R.string.changes_one_discarded_notification_ticker, discardedName, hours+"");
		
		sendNotification(title, text, ticker);
	}
	
	/**
	 * Method to show notification when many injured and one discarded in your teams.
	 * @param injured
	 * @param discardedName
	 * @param hours
	 */
	protected void showMoreAndOneNotifications(int injured, String discardedName, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_many_injured_one_discarded_notification_title);
		String text = context.getString(R.string.changes_many_injured_one_discarded_notification_text, injured+"", discardedName);
		String ticker = context.getString(R.string.changes_many_injured_one_discarded_notification_ticker, injured+"", discardedName, hours+"");
		
		sendNotification(title, text, ticker);
	}
	
	/**
	 * Method to show notification when many injured and many discarded in your teams.
	 * @param injured
	 * @param discarded
	 * @param hours
	 */
	protected void showMoreAndMoreNotifications(int injured, int discarded, long hours)
	{
		// notification texts
		String title = context.getString(R.string.changes_many_injured_many_discarded_notification_title);
		String text = context.getString(R.string.changes_many_injured_many_discarded_notification_text, injured+"", discarded+"");
		String ticker = context.getString(R.string.changes_many_injured_many_discarded_notification_ticker, injured+"", discarded+"", hours+"");
		
		sendNotification(title, text, ticker);
	}
	
	/**
	 * Method to show notification when no players are injured in your teams.
	 * The pending intent points to my team list.
	 * @param injuredPlayers
	 * @param hours
	 */
	protected void showDuetimeNotification( long hours )
	{
		// notification texts
		String title = context.getString(R.string.changes_duetime_notification_title);
		String text = context.getString(R.string.changes_duetime_notification_text, hours+"");
		String ticker = context.getString(R.string.changes_duetime_notification_ticker, hours+"");

		sendNotification(title, text, ticker);
	}
		
	protected void sendNotification(String title, String text, String ticker)
	{
		// get notification manager
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = 
			(NotificationManager) context.getSystemService(ns);
		
		// per quan creem notification
		long when = System.currentTimeMillis();

		// creem notification
		Notification notification = 
			new Notification(R.drawable.ic_stat_notification, ticker, when);
		
		// auto cancel flag added
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		
		// add sound notification
		notification.defaults |= Notification.DEFAULT_SOUND;
		
		// creem activity a llançar quan cliquem sobre la notification
		Intent notificationIntent = new Intent(context, MyTeamsListActivity.class)
			.setData(Supermanager.MyTeam.CONTENT_URI);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		// registrem el pending intent
		notification.setLatestEventInfo(context, title, text, contentIntent);
		
		// llancem notification
		mNotificationManager.notify(0, notification);
	}
}
